package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gulimall.product.dao.ProductAttrValueDao;
import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveProductAttr(List<ProductAttrValueEntity> collect) {
        this.saveBatch(collect);
    }

    @Override
    public List<ProductAttrValueEntity> listforSpu(Long spuId) {
        List<ProductAttrValueEntity> entityList = this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        return entityList;
    }

    @Override
    public void updatebySpuId(Long spuId, List<ProductAttrValueEntity> list) {
        // 1. 删除所有 spuId 关联的属性
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));

        // 2. 重新添加
        for (ProductAttrValueEntity valueEntity : list) {
            valueEntity.setSpuId(spuId);
        }
        this.saveBatch(list);
    }

}

